package cn.yudao.ymcc.web.controller;

import cn.yudao.ymcc.dto.KillPrmDto;
import cn.yudao.ymcc.service.IKillCourseService;
import cn.yudao.ymcc.domain.KillCourse;
import cn.yudao.ymcc.query.KillCourseQuery;
import cn.yudao.ymcc.result.JSONResult;
import cn.yudao.ymcc.result.PageList;
import cn.yudao.ymcc.vo.CourseInfoVo;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/killCourse")
public class KillCourseController {

    @Autowired
    public IKillCourseService killCourseService;


    /**
    * @Title: onlineOne
    * @Description: 获取秒杀活动的某个商品的信息
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/8/24 12:38
    * @Parameters:
    * @Return
    */
    @RequestMapping(value="/online/one/{courseId}/{activityId}",method= RequestMethod.GET)
    public JSONResult onlineOne(@PathVariable("courseId") Long courseId, @PathVariable("activityId") Long activityId){

        KillCourse killCourses = killCourseService.onlineOne(courseId,activityId);
        return JSONResult.success(killCourses);
    }

    /**
    * @Title: killOrder
    * @Description: 获取秒杀订单号--将订单信息存入redis
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/8/24 12:39
    * @Parameters:
    * @Return
    */
    @RequestMapping(value="/kill",method= RequestMethod.POST)
    public JSONResult killOrder(@RequestBody KillPrmDto dto){
        String killOrder = killCourseService.killOrder(dto);
        return JSONResult.success(killOrder);
    }

    /**
    * @Title: oneByOrder
    * @Description: 渲染订单页的数据
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/8/24 15:49
    * @Parameters:
    * @Return
    */
    @RequestMapping(value="/online/oneByOrder/{orderNo}",method= RequestMethod.GET)
    public JSONResult oneByOrder(@PathVariable("orderNo") String orderNo){
        CourseInfoVo courseInfoVo = killCourseService.oneByOrder(orderNo);
        return JSONResult.success(courseInfoVo);
    }

    /**
    * @Title: onlineAll
    * @Description: 获取redis中的秒杀活动的所有商品数据
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/8/24 10:54
    * @Parameters:
    * @Return
    */
    @RequestMapping(value="/online/all",method= RequestMethod.GET)
    public JSONResult onlineAll(){

       List<KillCourse> killCourses = killCourseService.onlineAll();

        return JSONResult.success(killCourses);
    }


    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody KillCourse killCourse){
        if(killCourse.getId()!=null){
            killCourseService.updateById(killCourse);
        }else{
            killCourseService.insert(killCourse);
        }
        return JSONResult.success();
    }

    /**
    * @Title: saveOrUpdate
    * @Description: 添加秒杀课程
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/8/23 15:02
    * @Parameters:
    * @Return
    */
    @RequestMapping(value="/add",method= RequestMethod.POST)
    public JSONResult add(@RequestBody KillCourse killCourse){

        killCourseService.add(killCourse);

        return JSONResult.success();
    }


    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        killCourseService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(killCourseService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(killCourseService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody KillCourseQuery query){
        Page<KillCourse> page = new Page<KillCourse>(query.getPage(),query.getRows());
        page = killCourseService.selectPage(page);
        return JSONResult.success(new PageList<KillCourse>(page.getTotal(),page.getRecords()));
    }
}
